from socketserver import BaseRequestHandler, TCPServer


# tcp应答服务器
class EchoHandler(BaseRequestHandler):
    def handle(self):
        print('Got connection from ', self.client_address)
        while True:
            msg = self.request.recv(8192)
            if not msg:
                break
            self.request.send(msg)


if __name__ == '__main__':
    # # 单进程单线程处理
    # serv = TCPServer(('',20000),EchoHandler)
    # serv.serve_forever()
    from threading import Thread
    NWORKERS = 16
    serv = TCPServer(('',20000),EchoHandler)
    for n in range(NWORKERS):
        t = Thread(target=serv.serve_forever)
        t.daemon = True # 守护线程
        t.start()
    serv.serve_forever()